// pages/Tabbar/Client/Client.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { Login } from '../../../store/user';
import { queryAgentCustomer } from '../../../api/user';

Page({
//  时间选择器
bindTimeChange:function(e){
  this.setData({
    date:e.detail.value,
    searchDate:e.detail.value,
  })
},
  //  搜索客户
searchClient(){
  wx.navigateTo({
    url:'/pages/User/SearchClient/SearchClient',
  });
},
// 交易明细
clientDealDetail(e){
  let item = e.target.dataset.item||e.currentTarget.dataset.item;
  wx.navigateTo({
    url:`/pages/User/ClientDetails/ClientDetails?item=${JSON.stringify(item)}`,
  });
},
  /**
   * 页面的初始数据
   */
  data: {
    client_total_amount:0,    // 客户总数
    client_desDeal_amount:0,  //未产生交易客户
    client_deal_amount:0,     //已产生交易客户
    startTime: '', // 开始时间
    endTime: '', // 结束时间
    customerName: '', // 客户名称
    clientList:[],    //客户列表
    token: wx.getStorageSync('token'),
    assetsAddress: assetsAddress,
    userRealnameStatus: wx.getStorageSync('realNameStatus'),
    shareTitle: app.globalData.global.shareTitle,
    showPrivacyAuthor: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setStorageSync('toPath', '/pages/Tabbar/Client/Client')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    if(wx.getStorageSync('hasNewUser') == 'true'){
      await Login({noToast: false});
      wx.setStorageSync('hasNewUser', 'falses')
    }
    this.setData({
      token: wx.getStorageSync('token'),
      userRealnameStatus: wx.getStorageSync('realNameStatus'),
    })
    if(wx.getStorageSync('realNameStatus') == '3'){
      this.getData()
    }
    var _this = this;
    if (wx.onNeedPrivacyAuthorization){
      wx.onNeedPrivacyAuthorization((resolve) => {
        console.log('触发弹窗')
        // 触发弹窗
        _this.setData({
          showPrivacyAuthor: true,
        })
        _this.resolvePrivacyAuthorization = resolve
        console.log(resolve,resolve);
      })
    }  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Tabbar/Home/Home?scene='+userId,
    }
  },
  // 获取客户信息
  getData(){
    let _this = this;
    queryAgentCustomer(_this.data.customerName,_this.data.startTime,_this.data.endTime)
    .then(res => {
      if(res.success){
        _this.setData({
          clientList: res.data.list ?? [],
          client_total_amount: res.data.countInfo ? res.data.countInfo.totalCount : 0,
          client_desDeal_amount: res.data.countInfo ? res.data.countInfo.transactionCount : 0,
          client_deal_amount: res.data.countInfo ? res.data.countInfo.noTransactionCount : 0,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  myChildEvent(e){
    console.log(e,'子组件触发')
    this.setData({
      token: wx.getStorageSync('token')
    })
  },
  // 展示时间选择器
  showPickTime(){
    const child = this.selectComponent('#pickDateTime')
    child.showPickTime();
  },
  // 时间选择器触发
  pickdateevent(e){
    console.log(e,'选择时间')
    this.setData({
      startTime: e.detail.startTime,
      endTime: e.detail.endTime,
      page: 1,
    })
    this.getData();
  },
  handleAgreePrivacyAuthorizationFun(){
    console.log('同意之后的回调');
  },
  disagreePrivacyAuthorization(){
    this.resolvePrivacyAuthorization({ event: 'disagree' });
  },
})